package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.dict.DictUtil;
import com.lam.common.exception.ParameterErrorException;
import com.lam.common.utils.CollectionUtil;
import com.lam.mall.entity.Area;
import com.lam.mall.entity.TransFree;
import com.lam.mall.entity.TransFreeCity;
import com.lam.mall.mapper.TransFreeCityMapper;
import com.lam.mall.mapper.TransFreeMapper;
import com.lam.mall.param.TransFreeParam;
import com.lam.mall.service.TransFreeService;

/**
 * 包邮项Service业务层处理
 * 
 * @author admin
 * @date 2023-04-21
 */
@Service
public class TransFreeServiceImpl  implements TransFreeService {

    @Autowired
    private TransFreeMapper transFreeMapper;
    @Autowired
    private TransFreeCityMapper transFreeCityMapper;

    @Override
	public List<TransFree> selectByTemplateId(String templateId) {
        List<TransFree> list = transFreeMapper.selectList(new QueryWrapper<TransFree>().eq("trans_fee_template_id", templateId));
        if(CollectionUtil.isNotEmpty(list)) {
        	DictUtil.convertJavaList(list);
        	for (TransFree free : list) {
        		free.setCityList(transFreeCityMapper.selectCityList(free.getId()));
			}
        }
		return list;
	}
    
    /**
     * 查询包邮项
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public TransFree selectById(String id) {

        return transFreeMapper.selectById(id);
    }

    /**
     * 查询包邮项列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<TransFree> selectList(TransFreeParam param) {
    
        QueryWrapper<TransFree> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(Objects.nonNull(param.getTransFeeTemplateId()), "trans_fee_template_id", param.getTransFeeTemplateId());

        return transFreeMapper.selectList(queryWrapper);
    }

    /**
     * 新增包邮项
     * 
     * @param transFree 
     * @return 
     */
    @Override
    @Transactional
    public int insert(TransFree transFree) {
    
    	transFreeMapper.insert(transFree);
    	
		List<Area> cityList = transFree.getCityList();
		if (CollectionUtil.isEmpty(cityList)) {
			throw new ParameterErrorException("请选择指定包邮城市");
		}
		// 当地址不为空时
		for (Area area : cityList) {
			TransFreeCity transFreeCity = new TransFreeCity();
			transFreeCity.setTransFreeId(transFree.getId());
			transFreeCity.setCityId(area.getId());
			transFreeCityMapper.insert(transFreeCity);
		}
		return 1;
    }

    /**
     * 修改包邮项
     * 
     * @param transFree 
     * @return 
     */
    @Override
    @Transactional
    public int update(TransFree transFree) {
    
        return transFreeMapper.updateById(transFree);
    }

    /**
     * 删除包邮项信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(List<String> idList) {
    
        if(CollectionUtil.isEmpty(idList)) {
            return 0;
        }
        
        transFreeMapper.deleteBatchIds(idList);
        
        // 删除所有指定包邮条件项和其包含的城市
        QueryWrapper<TransFreeCity> queryWrapper = new QueryWrapper<>();
        if(idList.size() == 1) {
        	queryWrapper.eq("trans_free_id", idList.get(0));
        }else {
        	queryWrapper.in("trans_free_id", idList);
        }
        transFreeCityMapper.delete(queryWrapper);
        
        return 1;
    }

}
